package com.education.welco.controller;


import com.education.common.pojo.AjaxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.education.common.core.BaseController;
import com.education.common.pojo.TStudent;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.StringUtils;
import com.education.welco.pojo.TClass;
import com.education.welco.pojo.TSchooldept;
import com.education.welco.pojo.TTeacher;
import com.education.welco.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 班级 信息操作处理
 *
 * @author d
 * @date 2020-05-08
 */
@RestController
@RequestMapping("/tClass")
public class TClassController extends BaseController {

    @Autowired
    private ITClassService classService;
    @Autowired
    private ITSchooldeptService schooldeptService;
    @Autowired
    private ITStudentService studentService;
    @Autowired
    private ITTeacherClassService itTeacherClassService;
    @Autowired
    private ITTeacherService itTeacherService;


    /**
     * 教务端 班级列表查询
     * 查询班级列表   班级名称 上级系的名称
     *
     * @param tClass
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list2")
    public ApiReturnObject list(TClass tClass, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<TClass> tClasses = classService.selectTClassListOther(tClass);
        for (TClass aClass : tClasses) {
            //格式化创建时间
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(aClass.getCreateTime());
            aClass.setCreateTimeStr(dateString);
        }
        PageInfo<TClass> pageInfo = new PageInfo<>(tClasses);
        return new ApiReturnObject("200", "查询成功", pageInfo);
    }

    /**
     * 查找某学校下的所有班级
     *
     * @param schoolId
     * @return
     */
    @GetMapping("/queryClassListAllForTea")
    public AjaxResult queryClassListAllForTea(Integer schoolId) {
        List<TClass> classList = classService.queryClassListAllForTea(schoolId);
        return AjaxResult.success(classList);
    }

    /**
     * 老师端班级列表查询  userId 老师id className 班级名称
     *
     * @param tClass
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/classListForTea")
    public ApiReturnObject classListForTea(@Validated TClass tClass, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        //List<TClass> tClasses = classService.selectTClassListOther(tClass);
        //根据老师id查找老师所代课的班级
        List<TClass> tClasses = classService.selectClassListByTeaId(tClass);

        for (TClass aClass : tClasses) {
            //格式化创建时间
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(aClass.getCreateTime());
            aClass.setCreateTimeStr(dateString);
        }
        PageInfo<TClass> pageInfo = new PageInfo<>(tClasses);
        return new ApiReturnObject("200", "查询成功", pageInfo);
    }

    @GetMapping("/selectOne")
    public ApiReturnObject selectOne(Integer id) {
        TClass tClass = new TClass();
        tClass.setId(id);
        TClass tClasses = classService.selectTClassListOther(tClass).get(0);
        //遍历组成部门id 数组
        ArrayList<Integer> arrayList = Lists.newArrayList();
        List<Integer> ids = getDeptIds(tClasses.getDeptId(), arrayList);
        tClasses.setClassIdList(ids);
        return new ApiReturnObject("200", "查询成功", tClasses);
    }

    public List<Integer> getDeptIds(Integer deptId, List<Integer> list) {
        TSchooldept tSchooldept = schooldeptService.selectTSchooldeptById(deptId);
        if (tSchooldept.getParentId() != 0) {
            //继续查上一级
            getDeptIds(tSchooldept.getParentId(), list);
        }
        list.add(deptId);
        return list;
    }

    //添加    选择
    @PostMapping("/add2")
    public ApiReturnObject add(@RequestBody TClass tClass) {
        if (tClass.getClassIdList() != null && tClass.getClassIdList().size() > 0)
            tClass.setDeptId(tClass.getClassIdList().get(tClass.getClassIdList().size() - 1));
        tClass.setCreateTime(new Date());
        //学校id
        TSchooldept tSchooldept = schooldeptService.selectTSchooldeptById(tClass.getDeptId());
        tClass.setSchoolId(tSchooldept.getSchoolId());

        classService.insertTClass(tClass);
        return new ApiReturnObject("200", "添加成功", "");
    }

    //修改
    @PostMapping("/update2")
    public ApiReturnObject update(@RequestBody TClass tClass) {
        if (tClass.getClassIdList() != null && tClass.getClassIdList().size() > 0)
            tClass.setDeptId(tClass.getClassIdList().get(tClass.getClassIdList().size() - 1));
        classService.updateTClass(tClass);
        return new ApiReturnObject("200", "修改成功", "");
    }


    //删除
    @GetMapping("/del2")
    public ApiReturnObject del(String ids) {
        //判断班级下面是否有学生
        TStudent tStudent = new TStudent();
        tStudent.setClassId(Integer.parseInt(ids));
        List<TStudent> tStudents = studentService.selectTStudentList(tStudent);
        if (tStudents.size() > 0) return new ApiReturnObject("201", "此班级已分配学生，无法删除", "");
        //删除班级前先删除教师班级中间表数据
        itTeacherClassService.deleteTTeacherClassByClassId(Integer.parseInt(ids));

        classService.deleteTClassByIds(ids);
        return new ApiReturnObject("200", "删除成功", "");
    }

    /*======================================================================================================*/

    /**
     * 根据班级查询其部门信息
     */
    @SuppressWarnings("all")
    public List<TClass> findDept(List<TClass> classList) {
        if (classList.size() > 0) {
            for (TClass tClass : classList) {
                TSchooldept schooldept = schooldeptService.selectTSchooldeptByClassId(tClass.getDeptId());
                //选择在授班级时上级部门查询错误修改
                if (StringUtils.isNotNull(schooldept)) {
                    tClass.setDeptId(schooldept.getId());
                    tClass.setDeptName(schooldept.getDeptName());
                }
            }
        }
        return classList;
    }


    /**
     * 根据部门id 查询班级列表
     */
    @GetMapping("list")
    public ApiReturnObject list(Integer deptId, Integer catalogId, Integer userId) {
        if (deptId != null) {
            //查询教师所授班级
            List<TClass> classes = classService.selectTClassByDeptId(deptId, catalogId, userId);
            List<TClass> classList = findDept(classes);
            return new ApiReturnObject("200", "查询成功", classList);
        }
        return new ApiReturnObject("201", "缺少deptId参数", "");
    }


    /**
     * 根据学校id 查询班级列表
     */
    @GetMapping("listClassBySchoolId")
    public ApiReturnObject listClassBySchoolId(Integer schoolId) {
        if (schoolId != null) {
            List<TClass> classes = classService.selectTClassAllBySchoolId(schoolId);
//            List<TClass> classList = findDept(classes);
            return new ApiReturnObject("200", "查询成功", classes);
        }
        return new ApiReturnObject("201", "缺少deptId参数", "");
    }


    /**
     * 根据部门id 查询班级列表
     */
    @GetMapping("listAll")
    public ApiReturnObject listAll(Integer deptId, Integer catalogId) {
        if (deptId != null) {
            List<TClass> classes = classService.selectTClassAllByDeptId(deptId);
            List<TClass> classList = findDept(classes);
            return new ApiReturnObject("200", "查询成功", classList);
        }
        return new ApiReturnObject("201", "缺少deptId参数", "");
    }


    /**
     * 根据教师userid 查询班级列表
     */
    @GetMapping("listAllList")
    public ApiReturnObject listAllList(Integer userId) {
        if (userId != null) {
            List<TClass> classes = classService.selectTClassAlluserId(userId);
            return new ApiReturnObject("200", "查询成功", classes);
        }
        return new ApiReturnObject("201", "缺少userId参数", "");
    }


    /**
     * 查询班级列表   资源查看班级使用
     */
    @GetMapping("listClassResource")
    public ApiReturnObject listClassResource(Integer catalogId, Integer resourceId) {
        if (catalogId != null) {
            List<TClass> classes = classService.selectTClassByCatalogId2(catalogId, resourceId);
            List<TClass> classList = findDept(classes);
            return new ApiReturnObject("200", "查询成功", classList);
        }
        return new ApiReturnObject("201", "缺少deptId参数", "");
    }

    /**
     * 查询班级列表   资源2查看班级使用
     */
    @GetMapping("listClassResourceTwo")
    public ApiReturnObject listClassResource2(Integer catalogId, String type, Integer catalogueId) {
        if (catalogId != null && type != null && !"".equals(type) && catalogueId != null) {
            TClass tClass = new TClass();
            tClass.setCatalogId(catalogId);
            tClass.setResourceType(type);
            tClass.setCatalogueId(catalogueId);
            List<TClass> classes = classService.selectTClassByCatalogId3(tClass);
            List<TClass> classList = findDept(classes);
            return new ApiReturnObject("200", "查询成功", classList);
        }
        return new ApiReturnObject("201", "缺少deptId参数", "");
    }

    /**
     * 查询班级列表   课程问答查看班级使用
     */
    @GetMapping("listAnswer")
    public ApiReturnObject listAnswer(Integer catalogId, Integer outId) {
        if (catalogId != null) {
            List<TClass> classes = classService.selectTClassByCatalogIdAnswer(outId, catalogId);
            List<TClass> classList = findDept(classes);
            return new ApiReturnObject("200", "查询成功", classList);
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }

    /**
     * 查询班级列表   录播视频查看班级使用
     */
    @GetMapping("listVideo")
    public ApiReturnObject listVideo(Integer catalogId, Integer resourceId) {
        if (catalogId != null) {
            List<TClass> classes = classService.selectTClassByCatalogIdVideo(resourceId, catalogId);
            List<TClass> classList = findDept(classes);
            return new ApiReturnObject("200", "查询成功", classList);
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }

    /**
     * 录播视频 已选查看班级
     */
    @GetMapping("/videoClass")
    public ApiReturnObject videoClass(Integer resourceId) {
        if (resourceId != null) {
            List<TClass> classList = classService.selectTClassByCatalogIdVideoAll(resourceId);
            if (classList != null && classList.size() > 0) {
                List<TClass> dept = findDept(classList);
                return new ApiReturnObject("200", "查询成功", dept);
            }
            return new ApiReturnObject("200", "结果为空", classList);
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }

    /**
     * 查询班级列表   课前预习查看班级使用
     */
    @GetMapping("listPreview")
    public ApiReturnObject listPreview(Integer catalogId, Integer outId) {
        if (catalogId != null) {
            List<TClass> classes = classService.selectTClassByCatalogIdPreview(outId, catalogId);
            List<TClass> classList = findDept(classes);
            return new ApiReturnObject("200", "查询成功", classList);
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }

    /**
     * 查询班级列表   课后作业查看班级使用
     */
    @GetMapping("listWork")
    public ApiReturnObject listWork(Integer catalogId, Integer outId) {
        if (catalogId != null) {
            List<TClass> classes = classService.selectTClassByCatalogIdWork(outId, catalogId);
            List<TClass> classList = findDept(classes);
            return new ApiReturnObject("200", "查询成功", classList);
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }

    /**
     * 查询班级列表   课后试题查看班级使用
     */
    @GetMapping("listQuest")
    public ApiReturnObject listQuest(Integer catalogId, Integer outId) {
        if (catalogId != null) {
            List<TClass> classes = classService.selectTClassByCatalogIdQuest(outId, catalogId);
            List<TClass> classList = findDept(classes);
            return new ApiReturnObject("200", "查询成功", classList);
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }

    @GetMapping("/selectclasslist")
    public ApiReturnObject selectclasslist(Integer userId, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        TTeacher tTeacher = itTeacherService.selectTTeacherById1(userId);
        Integer id = tTeacher.getId();
        List<Integer> integerList = itTeacherClassService.selectTTeacherClassByTeaId(id);
        ArrayList<Object> arrayList = new ArrayList<>();
        for (Integer integer : integerList) {
            TClass tClass = classService.selectTClassById(integer);
            arrayList.add(tClass);
        }

        return new ApiReturnObject("200", "查询成功", new PageInfo<>(arrayList));
    }


    //    @GetMapping("/selectclasslist")
//    public ApiReturnObject selectclasslist(Integer paperId){
//
//        return new ApiReturnObject("200","查询成功",);
//    }
    //查询班级学生个数
    @GetMapping("/selectstudentlist")
    public List<TStudent> selectstudentlist(Integer classId) {
        List<TStudent> tStudents = studentService.selectTStudentList1(classId);
        return tStudents;
    }

}